package com.abc.recycling.controller;

import com.abc.recycling.model.RecyclingStation;
import com.abc.recycling.service.RecyclingStationService;
import com.abc.recycling.mapper.RecyclingStationMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/stations")
@RequiredArgsConstructor
public class MapController {

    private final RecyclingStationService stationService;
    private final RecyclingStationMapper stationMapper;

    @GetMapping("/map")
    public String showMap(Model model) {
        try {
            log.info("显示回收站地图页面");
            // 获取所有营业中的回收站
            List<RecyclingStation> stations = stationMapper.findAllActive();
            log.info("找到{}个营业中的回收站", stations.size());
            stations.forEach(station -> 
                log.debug("回收站: {}, 地址: {}, 状态: {}", 
                    station.getName(), 
                    station.getAddress(), 
                    station.getStatus()));
            model.addAttribute("stations", stations);
            return "station/map";
        } catch (Exception e) {
            log.error("显示地图页面失败: {}", e.getMessage());
            throw e;
        }
    }

    @GetMapping("/map/nearby")
    @ResponseBody
    public ResponseEntity<List<RecyclingStation>> getNearbyStations(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam Integer radius) {
        return ResponseEntity.ok(stationService.getNearbyStations(latitude, longitude, radius));
    }
} 